/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.ComputerPlayer;
import model.Game;
import model.Player;

/**
 *
 * @author Dustin
 */
public class TableServlet extends HttpServlet {

    private GameServer gameServer = GameServer.getInstance();
    private Game game;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        game = (Game) session.getAttribute("game");

        if (request.getParameter("action").equals("tafelrefresh")) {
            if ((game != null && game.isLocked() && (game.isDealing() || !userIsTurn(request))) || (request.getParameter("force").equals("true"))) {
                if (game != null) {
                    game.getTable().hasChanged[game.getCurrentPlayerIndex()] = false;
                }
                RequestDispatcher dispatcher = request.getRequestDispatcher("/cards.jsp");
                dispatcher.forward(request, response);
                game.setIsDealing(false);
            }
        } else if (request.getParameter("action").equals("informatierefresh")) {
            RequestDispatcher dispatcher = request.getRequestDispatcher("/informatie.jsp");
            dispatcher.forward(request, response);
        } else {
            String actionCommand = request.getParameter("action");
            int handIndex = -1;
            int tableIndex = -1;

            if (request.getParameter("action").equals("start")) {
                while (game.getAmountOfPlayers() < 4) {
                    game.setPlayer(new ComputerPlayer("Computer " + game.getAmountOfPlayers()));
                }
                game.setLocked(true);
                game.deal();
            } else {
                if (actionCommand.equals("Wissel")) {
                    handIndex = Integer.parseInt(request.getParameter("selectOnder"));
                    tableIndex = Integer.parseInt(request.getParameter("selectMidden"));
                    game.getCurrentPlayer().swapCard(game.getTable(), handIndex, tableIndex);
                } else if (actionCommand.equals("Pas")) {
                    game.getCurrentPlayer().pass(game);
                } else if (actionCommand.equals("Wissel alles")) {
                    game.getCurrentPlayer().swapCards(game);
                }
                game.setNextPlayer();
                while(game.getAmountOfLosers() != 3 && game.getCurrentPlayer() instanceof ComputerPlayer) {
                    game.computerPlayerTurn();
                }
            }
        }
    }


    private boolean userIsTurn(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Player p = (Player) session.getAttribute("player");
        return game.getCurrentPlayer().equals(p);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        if (request.getParameter("action").equals("Aanmaken")) {
            Player p = (Player) session.getAttribute("player");

            String tableName = request.getParameter("tableName");
            boolean tableNameExists = false;
            for (int i = 0; i < gameServer.getGames().size() && !tableNameExists; i++) {
                if (gameServer.getGames().get(i).getTable().getName().equals(tableName)) {
                    tableNameExists = true;
                }
            }
            if (!tableNameExists) {
                game = new Game(tableName);
                game.setPlayer(p);
                gameServer.addGame(game);
                session.setAttribute("game", game);
                response.sendRedirect("/Eenendertigen/table.jsp");
            } else {
                //Waarschuwing tafel naam bestaat al. Opnieuw proberen
            }
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
